package oc_orm

import (
	"fmt"
	"oc_saas/internal/glo"
	oc_mysql "oc_saas/internal/initialize/db/orm/mysql"
	"oc_saas/internal/model/base"
	"oc_saas/internal/model/share"
	"os"

	"gorm.io/gorm"
)

type ocOrm struct{}

func (*ocOrm) InitOrm() {
	switch glo.OC_CONF.Server.DBType {
	// 观察现在是使用哪种类型的数据库
	case "mysql":
		glo.OC_DB = oc_mysql.OrmMysql()
	default:
		glo.OC_DB = nil
	}
}

func (*ocOrm) Migrate(db *gorm.DB) {
	if err := db.AutoMigrate(
		&base.Application{}, //应用表--管理租户
		&base.TenancyDB{},   //租户表
		&share.Rule{},
		&share.Api{},
		&share.Route{},
		&share.Role{},
		&share.RoleRule{},
		&share.User{},
		&share.RoleUser{},
	); err != nil {
		fmt.Println("数据库迁移失败", err)
		os.Exit(0)
	}
}

func (*ocOrm) SetAllDbs() {
	glo.OC_ORM_CLUSTER = oc_mysql.AllDbConnect(glo.OC_DB)
}

var OrmApp = new(ocOrm)
